package com.sskj.qiangjin.ui.activity;


import android.Manifest;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.sskj.common.base.App;
import com.sskj.common.base.GlideApp;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ImgUtil;
import com.sskj.common.util.PermissionTipUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.util.QRCodeUtil;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.bean.LinkBean;
import com.sskj.qiangjin.presenter.SpreadActivityPresenter;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;

@Route(path = RConfig.APP_SPREAD)//我的推广
public class SpreadActivity extends BaseActivity<SpreadActivityPresenter> {
    @BindView(R.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R.id.tv_invite_code)
    TextView tvInviteCode;
    @BindView(R.id.tv_copy)
    TextView tvCopy;
    @BindView(R.id.rl_common_title)
    RelativeLayout rl_common_title;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_spread;
    }

    @Override
    public SpreadActivityPresenter getPresenter() {
        return new SpreadActivityPresenter();

    }

    @Override
    protected void initView() {
        setTitle("分享赚钱");
        mPresenter.getLinkData();
        rl_common_title.setBackgroundColor(ContextCompat.getColor(this, R.color.app_872));

        ClickUtil.click(tvCopy, () -> {
            if (TextUtils.isEmpty(tvInviteCode.getText().toString())) {
                ToastUtil.showShort("请重新获取");
                return;
            }
            CopyUtil.copy(tvInviteCode.getText().toString());
        });
    }

    public void updataUI(LinkBean data) {
        tvInviteCode.setText("邀请码：" + data.getAccount());
        if (TextUtils.isEmpty(data.getQrc())) {
            ToastUtil.showShort("请重新获取");
            return;
        }
        GlideApp.with(App.INSTANCE).load(data.getQrc())
                .error(com.sskj.common.R.mipmap.app_icon_wolf)
                .placeholder(com.sskj.common.R.mipmap.app_icon_wolf)
                .skipMemoryCache(true) // 不使用内存缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE) // 不使用磁盘缓存
                .into(ivQrcode);
//        ImageUtil.setImage(data.getQrc(), ivQrcode);
//        QRCodeUtil.createImage(data.getQrc(), (int) RudenessScreenHelper.pt2px(this, 230), new QRCodeUtil.OnEncodeQRCodeCallback() {
//            @Override
//            public void onAnalyzeSuccess(Bitmap bitmap) {
//                ivQrcode.setImageBitmap(bitmap);
//            }
//
//            @Override
//            public void onAnalyzeFailed() {
//
//            }
//        });
        ClickUtil.click(ivQrcode, () -> {
            new RxPermissions(SpreadActivity.this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribe(isGrant -> {
                        if (isGrant) {
                            ImgUtil.saveImageToGallery(this, ivQrcode);
                        } else {
                            PermissionTipUtil.tip(SpreadActivity.this, "存储");
                        }
                    });
        });

    }
}

